package com.fagertveit.chainsawrus.tests;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import android.opengl.GLES20;
import android.util.Log;

import com.fagertveit.chainsawrus.Game;
import com.fagertveit.chainsawrus.Shader;
import com.fagertveit.chainsawrus.State;
import com.fagertveit.chainsawrus.geom.Rectangle;
import com.fagertveit.chainsawrus.graphics.Graphics;
import com.fagertveit.chainsawrus.graphics.Sprite;
import com.fagertveit.chainsawrus.graphics.SpriteSheet;
import com.fagertveit.chainsawrus.graphics.Texture;

public class GameTest extends State {
	
	int frames = 0;
	long lastUpdate;
	int fps = 0;

	//private FloatBuffer vertexBuffer;
	private Shader shader;
	private Texture texture;
	private Sprite sprite;
	
	private Rectangle rect3d = new Rectangle(-0.5f, -0.5f, 1.0f, 1.0f);
	private Rectangle rect2d = new Rectangle(200f, 200f, 200, 200);
	
	private float vertices[] = {
		100.0f, 100.0f, //Top Left Verts
		0.0f, 0.0f,    //Top Left Coords
		100.0f, 400.0f, //Bottom Left Verts
		0.0f, 1.0f,    //Bottom Left Coords
		400.0f, 100.0f, //Top Right Verts
		1.0f, 0.0f,    //Top Right Coords
		400.0f, 400.0f, //Bottom Right Verts
		1.0f, 1.0f     //Bottom Right Coords
	};
	
	private short indices[] = {
		0, 1, 2, 2, 1, 3
	};
	
	private FloatBuffer vertexBuffer;
	private ShortBuffer indexBuffer;
	
	private int positionLocation;
	private int resolutionLocation;
	private int texCoordLocation;
	private int colorLocation;
	private int textureLocation;

	private int program;
	
	public GameTest(Game game) {
		super(game);
		GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		
		shader = new Shader();
		shader.loadProgram("shaders/test2d.vshader", "shaders/test2d.fshader", game.getFileIO());
		program = shader.getProgram();
		rect3d.gen3dVerts();
		rect2d.genVerts();
		
		positionLocation = GLES20.glGetAttribLocation(program, "a_position");
        texCoordLocation = GLES20.glGetAttribLocation(program, "a_texCoord" );
		
		resolutionLocation = GLES20.glGetUniformLocation(program, "u_resolution");
		textureLocation = GLES20.glGetUniformLocation(program, "u_image");
		
		texture = new Texture("img/sprite2.png");
		texture.load(game.getFileIO());
		
		
		vertexBuffer = ByteBuffer.allocateDirect(vertices.length * 4)
        .order(ByteOrder.nativeOrder()).asFloatBuffer();
		vertexBuffer.put(vertices).position(0);
		
		indexBuffer = ByteBuffer.allocateDirect(indices.length * 2)
        .order(ByteOrder.nativeOrder()).asShortBuffer();
		indexBuffer.put(indices).position(0);
		
		sprite = new Sprite(50, 50, 100, 100);
		sprite.genUV(512, 512);
	}

	@Override
	public void update(long delta) {
		// TODO Auto-generated method stub
		if(lastUpdate < 0)
		{
			fps = frames;
			frames = 0;
			lastUpdate = 1000000000;
			Log.d("FPS", "FPS: " + fps);
		}
		else
		{
			frames++;
			lastUpdate -= delta;
		}
	}

	@Override
	public void render(Graphics g, long delta) {
		GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
		
		GLES20.glUseProgram(program);
		
		GLES20.glUniform2f(resolutionLocation, game.getWidth(), game.getHeight());
		
		vertexBuffer.position(0);
	    GLES20.glVertexAttribPointer(positionLocation, 2, GLES20.GL_FLOAT, false, 16, vertexBuffer);
        
	    vertexBuffer.position(2);
        GLES20.glVertexAttribPointer(texCoordLocation, 2, GLES20.GL_FLOAT, false, 16, vertexBuffer);
        
        GLES20.glEnableVertexAttribArray(1);
        GLES20.glEnableVertexAttribArray(0);
        
     // Bind the base map
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture.getTextureId());
        // Set the base map sampler to texture unit to 0
        GLES20.glUniform1i(textureLocation, 0);

        GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, indexBuffer);
        
        sprite.render(10, 10);
        //for(int i = 0; i < 10; i++) {
        	//sprite.render((float)(Math.random() * 600), (float)(Math.random() * 400));
        //}
        
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

}
